package com.tianzheng.rzzl.base;

import android.content.Context;

import androidx.multidex.MultiDex;

import com.alibaba.android.arouter.launcher.ARouter;
import com.amap.api.maps.MapsInitializer;
import com.tianzheng.rzzl.BuildConfig;
import com.basic.library.base.BaseApplication;
import com.basic.library.view.ClockTickUtil;
import com.tzlibrary.imageSelector.upload.OssUploadImagesHelper;


public class AppBaseApplication extends BaseApplication {

    private String OSS_URL = "http://oss-cn-hangzhou.aliyuncs.com";
    private String OSS_DIR_Image = "QyAfterSale/img/";
    private String OSS_DIR_File = "QyAfterSale/file/";
    private String BUCKET_NAME = "xinhui2017";
    private String Oss_accessKeyId = "LTAI5tLsMveMPP93vXdzky8e";
    private String Oss_accessKeySecret = "VPWNTDcdVEl6nlJZehgcykfrLpZlKL";
    private String BUGLY_APPID = "38c7e0b21c"; //腾讯bugly

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        MultiDex.install(this);
    }

    public static String version;

    public void onCreate() {
        super.onCreate();
//        setArouter();

//        //日志开关
//        UMConfigure.setLogEnabled(BuildConfig.DEBUG);
//        //预初始化
//        PushHelper.preInit(this);
//        //后台定位初始化
//        UtilsContextOfGaode.init(this);
//        //隐私策略
//        boolean privacyAgree = true;
//        if (privacyAgree) {
//            initPush();
//        }
    }

    public void initPush() {
        if (PushHelper.isMainProcess(this)) {
            new Thread(() -> PushHelper.init(getApplicationContext())).start();
        } else {
            PushHelper.init(getApplicationContext());
        }
    }

    @Override
    protected void initInMainProcess() {
        OssUploadImagesHelper.Companion.getInstance().init(this, OSS_URL, BUCKET_NAME, Oss_accessKeyId, Oss_accessKeySecret, OSS_DIR_Image);
        OssUploadImagesHelper.Companion.getInstance().setFolders(OSS_DIR_Image, OSS_DIR_File);
        MapsInitializer.updatePrivacyShow(this, true, true);
        MapsInitializer.updatePrivacyAgree(this, true);

        ClockTickUtil.INSTANCE.registerReceiver(this);
        //初始化腾讯bugly
//        CrashReport.initCrashReport(getApplicationContext(), BUGLY_APPID, BuildConfig.DEBUG);
    }

    private void setArouter() {
        if (BuildConfig.DEBUG) {   // 这两行必须写在init之前，否则这些配置在init过程中将无效
            ARouter.openLog();     // 打印日志
            ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        }
        ARouter.init(this); // 尽可能早，推荐在Application中初始化
    }

    public static AppBaseApplication getInstance() {
        if (instance == null)
            throw new NullPointerException("onCreate中添加instance = this");
        return (AppBaseApplication) instance;
    }

    @Override
    protected String getBaseUrl() {
        return BuildConfig.API_SERVER_URL;
    }

    @Override
    protected BaseApplication initInstance() {
        return this;
    }

    @Override
    public void exit() {
//        for (Activity activity : activityList) {
//            activity.finish();
//        }
//        handler.postDelayed(() -> SophixManager.getInstance().killProcessSafely(), 600);
        super.exit();
    }
}
